package light.mvc.controller.voter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import light.mvc.controller.base.BaseController;
import light.mvc.dao.BaseDaoI;
import light.mvc.model.sys.Tuser;
import light.mvc.model.sys.Tvoterinfo;
import light.mvc.model.voter.Tfinishvoter;
import light.mvc.model.voter.Tvoter;
import light.mvc.pageModel.base.Json;
import light.mvc.pageModel.base.Tree;
import light.mvc.pageModel.sys.User;
import light.mvc.service.sys.UserServiceI;
import light.mvc.service.voter.VoterServiceI;

@Controller
@RequestMapping("/voter")
public class VoterController extends BaseController{
	
	@Autowired
	private VoterServiceI vsi;
	@Autowired
	private UserServiceI usi;
	
	@RequestMapping("/index")
	public String index(){
		return "voter/index";
	}
	
	@RequestMapping("/dataGrid")
	@ResponseBody
	public List<Tvoter> find(Tvoter t){
		List<Tvoter> list = vsi.find(t);
		return list;
	}
	
	@RequestMapping("/findGrid")
	@ResponseBody
	public List<Tvoter> findGrid(Tvoterinfo t){
		if(t.getUser_id()==1){
			t.setUser_id(null);
		}
		List<Tvoter> list = vsi.find(t);
		return list;
	}
	
	@RequestMapping("/addOrUpdatePage")
	public String addPage(HttpServletRequest req){
		String id = req.getParameter("id");
		if(id!=null){
			Long i = Long.parseLong(id);
			Tvoter t = new Tvoter();
			t.setId(i);
			t = vsi.get(t);
			req.setAttribute("t", t);
		}
		return "voter/voterAddOrUpdate";
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Json add(Tvoter t) {
		Json j = new Json();
		try {
			vsi.saveOrUpdate(t);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg(e.getMessage());
			e.printStackTrace();
		}
			
		return j;
	}
	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(Tvoter t) {
		Json j = new Json();
		try {
			vsi.delete(t);
			j.setSuccess(true);
			j.setMsg("删除成功！");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg(e.getMessage());
			e.printStackTrace();
		}
		
		return j;
	}
	
	
	
	@RequestMapping("/tree")
	@ResponseBody
	public List<Tree> tree(Tuser t) {
		return vsi.tree(t);
	}
	
	@RequestMapping("/check")
	public String check(){
		return "/voter/check";
	}
	
	@RequestMapping("/gotovoter")
	public String gotovoter(Tvoter t,HttpServletRequest req){
		req.setAttribute("vi_id", t.getVi_id());
		t.setVi_id(null);
		t = vsi.get(t);
		req.setAttribute("t", t);
		String cs = t.getCandidate();
		if(cs!=null){
			String c[] = cs.split(",");
			List<String> list = new ArrayList<String>();
			for(int i=0;i<c.length;i++){
				String id = c[i];
				User u = usi.get(Long.parseLong(id));
				String str = "<input type=\"radio\" name=\"candidate\" style=\"padding: 10px\" value=\""+u.getId()+"\">"+u.getName();
				list.add(str);
			}
			req.setAttribute("lis", list);
		}
		return "/voter/gototvoter";
	}
	
	@RequestMapping("/gotovoterByMobile")
	@ResponseBody
	public Tvoter gotovoterByMobile(Tvoter t){
		t.setVi_id(null);
		t = vsi.get(t);
		return t;
	}
	
	@RequestMapping("/findCandidate")
	@ResponseBody
	public List<User> findCandidate(Tvoter t){
		List<User> list = new ArrayList<User>();
		String tt = t.getCandidate();
		if(tt!=null){
			String cs[] = tt.split(",");
			for(String c:cs){
				User user = usi.get(Long.parseLong(c));
				list.add(user);
			}
		}
		return list;
	}
	
	@RequestMapping("/finishVoter")
	public String finishVoter(Tvoter t,HttpServletRequest req){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Tfinishvoter tf = new Tfinishvoter();
		tf.setVi_id(t.getVi_id());
		tf = vsi.getfh(tf);
		User u = usi.get(tf.getUser_id());
		req.setAttribute("date", sdf.format(tf.getCreatedate()));
		req.setAttribute("name", u.getName());
		t.setVi_id(null);
		t = vsi.get(t);
		req.setAttribute("t", t);
		return "voter/finishvoter";
	}
	
	@RequestMapping("/finishVoterByMobile")
	@ResponseBody
	public Tvoter finishVoterByMobile(Tvoter t){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Tfinishvoter tf = new Tfinishvoter();
		tf.setVi_id(t.getVi_id());
		tf = vsi.getfh(tf);
		User u = usi.get(tf.getUser_id());
		t.setVi_id(null);
		t = vsi.get(t);
		if(t!=null){
			t.setVoter(u.getName());
			t.setCandidate(sdf.format(tf.getCreatedate()));
		}
		return t;
	}
	
	@RequestMapping("/countVoter")
	public String countVoter(Tvoter t,HttpServletRequest req){
		req.setAttribute("id", t.getId());
		return "voter/countVoter";
	}
	
	@RequestMapping("/saveVoter")
	@ResponseBody
	public Json saveVoter(HttpServletRequest req){
		Json j = new Json();
		try {
			String id = req.getParameter("id");
			String voters = req.getParameter("voters"); 
			vsi.updateVoterInfo(id,voters);
			j.setSuccess(true);
			j.setMsg("保存成功！");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("保存失败");
			e.printStackTrace();
		}
			
		return j;
	}
	
	@RequestMapping("/count")
	public String count(){
		return "voter/count";
	}
	
	
	@RequestMapping("/getColumn")
	@ResponseBody
	public Map<String,Object> getColumn(Tvoter t){
		return vsi.getColumn(t);
	}
	
	@RequestMapping("/getPie")
	@ResponseBody
	public String getPie(Tvoter t){
		return vsi.getPie(t);
	}
	

}
